<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
</head>
<body>
	<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
		<fieldset>
			<div class="form-group">
				<label class="col-md-2 control-label">标题</label>
				<div class="col-md-10">
					<input class="form-control" placeholder="标题" type="text" name="subject" id="subject" maxlength="255">
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-2 control-label">收件人</label>
				<div class="col-md-10">
					<input class="form-control" placeholder="收件人" type="text" name="toEmail" id="toEmail" maxlength="128">
				</div>
			</div>

			<div class="form-group">
				<label class="col-md-2 control-label">内容</label>
				<div class="col-md-10">
					<textarea class="layui-textarea" id="demo" style="display: none">  
					</textarea>
				</div>
			</div>

			<div class="form-actions">
				<div class="row" align="center">
					<div class="col-md-12">
					    <button class="btn btn-primary" onclick="location.href='mailList.html'">返回</button>
						<button class="btn btn-primary" type="submit" onclick="add(this, false)">
							<i class="fa fa-save"></i> 存草稿
						</button>
						<button class="btn btn-primary" type="submit" onclick="add(this, true)">
							<i class="fa fa-save"></i> 发送
						</button>
					</div>
				</div>
			</div>

		</fieldset>
	</div>

	<script src="../../js/constant.js"></script>
	<script type="text/javascript" src="../../js/libs/jquery-3.3.1.min.js"></script>
	<script type="text/javascript" src="../../js/jq.js"></script>
	<script type="text/javascript" src="../../layui/layui.js"></script>
	<script type="text/javascript" src="../../js/common.js"></script>
	<script type="text/javascript">
		var layedit, index;
		layui.use(['layedit','upload'], function(){
		  layedit = layui.layedit;
		  layedit.set({
              uploadImage: {
                  url: domainName + '/zuul/api-f/files/layui?access_token='+localStorage.getItem("access_token"),
                  type: 'post'
              }
            });
		  index = layedit.build('demo');
		});
	
		function add(obj, send) {
			var subject = $("#subject").val();
			var toEmail = $("#toEmail").val();
		    if(subject.trim() == ""){
		       layer.msg("标题不能为空")
			   return;
		    }
		    if(toEmail.trim() == ""){
		       layer.msg("收件人不能为空")
			   return;
		    }
		    $(obj).attr("disabled",true);
		    
		    var formdata = $("#form").serializeObject();
		    formdata.subject = subject;
		    formdata.toEmail = toEmail;
		    formdata.content = layedit.getContent(index);

			$.ajax({
				type : 'post',
				url : domainName + '/api-b/mails?send=' + send,
				contentType: "application/json; charset=utf-8",  
				data : JSON.stringify(formdata),
				success : function(data) {
				    var msg = "成功";
				    if(send) {
				        msg = "异步发送邮件，请在列表中查看结果";
					}
					layer.msg(msg, {shift: -1, time: 1000}, function(){
						location.href = "mailList.html";
                    });
				}
			});
            $(obj).attr("disabled",false);
		}
		
	</script>
</body>
</html>